package com.gzhu.knowledgeAdmin.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzhu.knowledgeAdmin.common.exception.BusinessException;
import com.gzhu.knowledgeAdmin.common.exception.GuliException;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.common.vo.CollectionVo;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import com.gzhu.knowledgeAdmin.entity.User;

import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gzhu.knowledgeAdmin.dao.XtyhscDao;
import com.gzhu.knowledgeAdmin.entity.Xtyhsc;
import com.gzhu.knowledgeAdmin.service.XtyhscService;
import org.springframework.util.StringUtils;


@Service("xtyhscService")
public class XtyhscServiceImpl extends ServiceImpl<XtyhscDao, Xtyhsc> implements XtyhscService {



    @Override
    public PageVo queryPage(Map<String, Object> params) {
        // 获取当前页和每页大小
        long pageNo = Long.parseLong((String) params.get("page"));
        long pageSize = Long.parseLong((String) params.get("limit"));
        String keyWord = (String) params.get("keyWord");
        String zsflId = (String) params.get("zsflId");


        Page<CollectionVo> page = new Page<>(pageNo, pageSize);
        QueryWrapper<CollectionVo> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(keyWord)) {
            wrapper.like("z.F_ZSBT", keyWord);
        }
        if(!StringUtils.isEmpty(zsflId)){
            wrapper.eq("z.F_ZSFLID",Integer.parseInt(zsflId));
        }

        User curUser = UserInfoUtils.getCurrentUser();
        wrapper.eq("x.F_YHID", curUser.getUserId());
        page = baseMapper.page(page,wrapper);

        // 封装新page对象
        Page<CollectionVo> collectionVoPage = new Page<>();
        collectionVoPage.setRecords(page.getRecords());
        collectionVoPage.setTotal(page.getTotal());
        collectionVoPage.setSize(page.getSize());
        collectionVoPage.setCurrent(page.getCurrent());
        collectionVoPage.setPages(page.getPages());

        return new PageVo(collectionVoPage);
    }


    /**
     * 收藏知识
     *
     * @param id
     */
    @Override
    public void addCollection(Integer id) {

        Integer userId = UserInfoUtils.getCurrentUser().getUserId();
        // 判断是否已经收藏过了
        Integer count = baseMapper.selectCount(new QueryWrapper<Xtyhsc>().eq("F_ZSID", id).eq("F_YHID", userId));
        if (count > 0) {
            throw new BusinessException(ErrorCode.COLLECTED_ERROR);
        }


        // 封装对象
        Xtyhsc xtyhsc = new Xtyhsc();
        xtyhsc.setZsid(id);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        xtyhsc.setScsj(timestamp);
        xtyhsc.setYhid(userId);

        // 添加收藏
        baseMapper.insert(xtyhsc);

    }



}